package com.wp.rcm.controller;
import com.wp.rcm.pojo.Services;
import com.wp.rcm.pojo.User;
import com.wp.rcm.service.ServicesService;
import com.wp.rcm.service.UserService;
import com.wp.rcm.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/service")
public class ServicesController {
    @Resource
    private ServicesService servicesService;

    @Resource
    private UserService userService;

      @RequestMapping("/dispatch")
    public String dispatch(@RequestParam(required = false) String svrCustName,@RequestParam(required = false) String svrTitle,@RequestParam(required = false) String svrType, Model model, Integer currPageNo){
          Integer page1 = currPageNo==null?1:currPageNo;

          Page page = new Page();
          page.setTotal(servicesService.count(svrCustName,svrTitle,svrType)); // 数据总条数
          if (page1<1){
              page1=1;
              model.addAttribute("page","已经是最顶页码");
          }
          if (page1>page.getPages()){
              page1=page.getPages();
              model.addAttribute("page","已经是最后一页");
          }
          page.setCurrent(page1); // 设置偏移量

          // 存储数据
          List<Services> all = servicesService.finAll(svrCustName,svrTitle,svrType, page.getOffset(), page.getPageSize());
          page.setList(all);

          model.addAttribute("dispatchPage",page);

          List<User> user = userService.findAll();

          model.addAttribute("users",user);

          return "service/dispatch";
    }


    // 处理删除
    @RequestMapping("/deal")
    public String deal(String svrCustName, String svrTitle, String svrType, Model model,Integer currPageNo){
        Integer page1 = currPageNo==null?1:currPageNo;

        Page page = new Page();
        page.setTotal(servicesService.count(svrCustName,svrTitle,svrType)); // 数据总条数
        page.setCurrent(page1); // 设置偏移量

        // 存储数据
        List<Services> all = servicesService.finAll(svrCustName,svrTitle,svrType, page.getOffset(), page.getPageSize());
        page.setList(all);

        model.addAttribute("dealPage",page);
        return "service/deal";
    }


    // 处理
    @RequestMapping("/feedback")
    public String feedback(String svrCustName, String svrTitle, String svrType, Model model,Integer currPageNo){
        Integer page1 = currPageNo==null?1:currPageNo;

        Page page = new Page();
        page.setTotal(servicesService.count(svrCustName,svrTitle,svrType)); // 数据总条数
        page.setCurrent(page1); // 设置偏移量

        // 存储数据
        List<Services> all = servicesService.finAll(svrCustName,svrTitle,svrType, page.getOffset(), page.getPageSize());
        page.setList(all);

        model.addAttribute("feedbackPage",page);

        return "service/feedback";
    }

    @RequestMapping("/arch")
    public String arch(String svrCustName, String svrTitle, String svrType, Model model,Integer currPageNo){
        Integer page1 = currPageNo==null?1:currPageNo;

        Page page = new Page();
        page.setTotal(servicesService.count(svrCustName,svrTitle,svrType)); // 数据总条数
        page.setCurrent(page1); // 设置偏移量

        // 存储数据
        List<Services> all = servicesService.finAll(svrCustName,svrTitle,svrType, page.getOffset(), page.getPageSize());
        page.setList(all);

        model.addAttribute("archPage",page);

        return "service/arch";
    }


    @RequestMapping("/toAdd")
    public String add(){
        return "service/add";
    }

    @RequestMapping("/findArch/{chcId}")
    public String findArch(@PathVariable(name = "chcId") Integer id,Model model){
        Services services = servicesService.findId(id);

        model.addAttribute("serviceFindArch",services);

        return "service/findArch";
    }

    @RequestMapping("/toedit/{chcId}")
    public String edit(@PathVariable(name = "chcId") Integer id,Model model){
        Services services = servicesService.findId(id);
        model.addAttribute("serviceEdit",services);
        return "service/edit";
    }

    @RequestMapping("/add")
    public String add(Services services,Model model){
        int add = servicesService.addService(services);
        if (add>0){
            return "redirect:/service/feedback";
        }else {
            return "service/add";
        }
    }


    @RequestMapping("/edit")
    public String edit(Services services,Model model){
        int update = servicesService.updaService(services);
        if (update>0){
            return "redirect:/service/feedback";
        }else {
            return "service/edit";
        }
    }

    @RequestMapping("/del/{svrId}")
    @ResponseBody  // 处理json格式
    public String del(@PathVariable(name = "svrId") Integer id,Model model){
        int del = servicesService.delServices(id);
        return "true";
    }
}
